/*
 * Copyright (c) 2011, Fredrik Häggström
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the Luleå University of Technology nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef PID_H_
#define PID_H_

#include <tT.h>
#include <env.h>
#include "can.h"

typedef struct pid_t
{
	tt_object_t super;
	tt_object_t *target;
	tt_method_t function;
	int32_t output;
	int32_t min;
	int32_t max;
	int32_t K;
	int32_t Ti;
	int32_t Td;
	int32_t r;
	int32_t e;
	int32_t Isum;
	int32_t dt;
	int32_t prev;
	int8_t enable;
	volatile int32_t *input;
	//can_listner_t listner;
} pid_t;

extern pid_t pid;
extern uint32_t mode;

#define pid(target,func,min,max,K,Ti,Td,dt,input) {tt_object(),(tt_object_t *)target,(tt_method_t)func,0,min,max,(K * 10),( Ti * 10000), (Td * 100000),0,0,0, (dt * 1000000),0,0, input}

env_result_t pid_run( pid_t *self, void *none);
env_result_t pid_reset( pid_t *self, void *none);
env_result_t pid_decode( pid_t *self, int32_t data[2]);
env_result_t pid_init(pid_t *self, void *none);

#endif
